package com.ruoyi.web.controller.trade;

import com.github.pagehelper.PageInfo;
import com.hundun.vision.api.dto.ExchangeDTO;
import com.hundun.vision.api.dto.RobotDTO;
import com.hundun.vision.api.dto.StrategyDTO;
import com.hundun.vision.api.query.RobotQuery;
import com.hundun.vision.api.request.RobotRequest;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.service.RobotService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：RuoChen
 * @date ：20:24 2021/3/2
 * @description：机器人控制器
 */
@RestController
@RequestMapping("/trade/robot")
public class RobotController extends BaseController {

    @Autowired
    private RobotService robotService;

    @PreAuthorize("@ss.hasPermi('trade:robot:save')")
    @Log(title = "机器人", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody RobotRequest request) {
        return toAjax(robotService.save(request));
    }

    @PreAuthorize("@ss.hasPermi('trade:robot:remove')")
    @Log(title = "机器人", businessType = BusinessType.DELETE)
    @DeleteMapping("/remove/{robotIds}")
    public AjaxResult remove(@PathVariable List<Long> robotIds) {
        return toAjax(robotService.removeByIds(robotIds));
    }

    @PreAuthorize("@ss.hasPermi('trade:robot:update')")
    @Log(title = "机器人", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult update(@RequestBody RobotRequest request) {
        return toAjax(robotService.update(request));
    }

    @PreAuthorize("@ss.hasPermi('trade:robot:get')")
    @GetMapping(value = {"/", "/{robotId}"})
    public AjaxResult get(@PathVariable(value = "robotId", required = false) Long robotId) {
        AjaxResult ajax = AjaxResult.success();
        List<ExchangeDTO> exchanges = robotService.getExchanges();
        List<StrategyDTO> strategies = robotService.getStrategies();
        ajax.put("exchanges", exchanges);
        ajax.put("strategies", strategies);
        if (StringUtils.isNotNull(robotId)) {
            ajax.put(AjaxResult.DATA_TAG, robotService.getById(robotId));
        }
        return ajax;
    }

    @PreAuthorize("@ss.hasPermi('trade:robot:search')")
    @GetMapping("/search")
    public TableDataInfo search(RobotQuery query) {
        PageInfo<RobotDTO> pageInfo = robotService.search(query);
        return getDataTable(pageInfo);
    }

    @PreAuthorize("@ss.hasPermi('trade:robot:start')")
    @GetMapping(value = "/start/{robotIds}")
    public AjaxResult resume(@PathVariable List<Long> robotIds) {
        return toAjax(robotService.start(robotIds));
    }

    @PreAuthorize("@ss.hasPermi('trade:robot:pause')")
    @GetMapping(value = "/pause/{robotIds}")
    public AjaxResult pause(@PathVariable List<Long> robotIds) {
        return toAjax(robotService.pause(robotIds));
    }
}
